package org.simplesocket.simplenettycore.handlers;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.simplesocket.simplenettycore.context.SimpleNettyContext;

public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    
    private final SimpleNettyContext context;
    
    public ServerChannelInitializer(SimpleNettyContext context) {
        this.context = context;
    }
    
    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast("LengthConvert",
                new LengthFieldBasedFrameDecoder((1024 * 1024) * 50,
                        0, 4, 0, 0));// 入站1
        
        pipeline.addLast("ConnectLifecycleHandler",
                PipelineInitializer.getConnectLifecycleHandler(context));// 非出站入站
        
        pipeline.addLast("ProtocolCodecHandler",
                PipelineInitializer.getProtocolCodecHandler());// 入站2、出站3
        
        pipeline.addLast("EncryptHandler", PipelineInitializer.getEncryptHandler());// 出站2
        
        pipeline.addLast("DecryptHandler", PipelineInitializer.getDecryptHandler());// 入站3
        
        pipeline.addLast("DistributeRouteHandler",
                PipelineInitializer.getDistributeRouteHandler(context));// 入站4、出站1
    }

}
